var AIW_number = {0:'0', 1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9'}
var AIW_unitN = ['兆','亿','万','个']
var AIW_unitP = ['厘','分','角']
var AIW_unitU = ['千','百','十','个']
var voiceKey = {
  '0':'0', 
  '1':'1', 
  '2':'2', 
  '3':'3', 
  '4':'4', 
  '5':'5', 
  '6':'6', 
  '7':'7', 
  '8':'8', 
  '9':'9',
  '十':'10',
  '百':'11','千':'12','万':'13','点':'14','收款':'15','元':'16','支出':'17'};

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function formatYYYYMMDD (timestamp) {
  let time = new Date(timestamp)
  let year = time.getFullYear()
  let month = time.getMonth() + 1
  let date = time.getDate()
  let hours = time.getHours()
  let minute = time.getMinutes()
  let second = time.getSeconds()

  if (month < 10) { month = '0' + month }
  if (date < 10) { date = '0' + date }
  if (hours < 10) { hours = '0' + hours }
  if (minute < 10) { minute = '0' + minute }
  if (second < 10) { second = '0' + second }
  return year + '年' + month + '月' + date + '日'
}

function amountInWords(amount) {
  var AIW = (amount+'').split('.')
  var result;

  if(AIW.length>1) {
    result = amountInWordsRound(AIW[0])+'点'+AIW_number[AIW[1]]+'元'
  } else {
    result = amountInWordsRound(AIW[0])+'元'
  }

  return result;
 }

 function amountInWordsRound(round) {
  if(!round) return ''
  var words = ''
  var rounds = Array.from(round)
  var length = rounds.length
  var unitU = JSON.parse(JSON.stringify(AIW_unitU))
  var unitN = JSON.parse(JSON.stringify(AIW_unitN))

  while(length>0) {
      length--
      var A = rounds.pop()
      var U = unitU.pop()
      var N = (unitU.length==3?unitN.pop():'')
      if(A!=0) words=AIW_number[A]+U+N+words
      else words=AIW_number[A]+N+words
      if(unitU.length==0) unitU = JSON.parse(JSON.stringify(AIW_unitU))
      words = words.replace('undefined','')
      words = words.replace('个','')
  }

  return amountInWordsFormate(words)
}

function amountInWordsF(fix) {
  if(!fix) return ''
  var unitP = JSON.parse(JSON.stringify(AIW_unitP));
  var fixs = Array.from(fix)
  var words=''
  fixs.forEach(function(i,o){
    var P = unitP.pop()
    if(o==0) words+=AIW_number[o]
    else words+=AIW_number[o]+P
    words = words.replace('undefined','')
  })

  return amountInWordsFormate(words)
}

function amountInWordsFormate(words) {
  //去除中位零
  var length = words.length
  while(length>1) {
      length--
      var now = words.charAt(length)
      var before = words.charAt(length-1)
      var after =  words.charAt(length+1)
      if(now == before || (AIW_unitN.includes(after) && now==AIW_number[0] ) ){
          words =  words.substring(0,length)+words.substring(length+1)
          length = words.length
      }
  }
  //去除末位零
  length =  2
  while(length>1) {
      length--
      if(words.charAt(words.length-1)==AIW_number[0] && words.length>1){
          words =  words.substring(0,words.length-1)
          length = 2
      }
  }
  return words
}

function parsePriceBytes(price) {
  var s = price.split(".");
  var i = s[0];
  var d = s[1];
  i = i.padStart(8, "0")
  console.log(i);

  var bytes = [];
  bytes.push(0x0A);

  var i1 = "0x" + i.substr(0, 2); 
  bytes.push(parseInt(i1))

  var i2 = "0x" + i.substr(2, 2); 
  bytes.push(parseInt(i2))

  var i3 = "0x" + i.substr(4, 2); 
  bytes.push(parseInt(i3))

  var i4 = "0x" + i.substr(6, 2); 
  bytes.push(parseInt(i4))

  var i5;
  if (d == undefined) {
    i5 = "0x10"; 
  } else {
    i5 = "0x1" + d; 
  }
  bytes.push(parseInt(i5))
  bytes.push(0x44);

  console.log(bytes);
  return bytes;
}

function parseVoiceBytes(price){  
  var convert = amountInWords(price);
  convert = convert.padEnd(17, "0");

  console.log("1----" + convert);

  var bytes = [];
  bytes.push(0x42);
  bytes.push(0x37);
  bytes.push(0x3A);

  bytes.push(0x31);
  bytes.push(0x35);

  for (var i = 0 ; i < convert.length; i++) {
    var e = convert[i];

    if (e == '0') {
      bytes.push(0x00);
      bytes.push(0x00);
    } else if (e == '1'){
      bytes.push(0x30);
      bytes.push(0x31);
    } else if (e == '2'){
      bytes.push(0x30);
      bytes.push(0x32);
    } else if (e == '3'){
      bytes.push(0x30);
      bytes.push(0x34);
    } else if (e == '4'){
      bytes.push(0x30);
      bytes.push(0x34);
    } else if (e == '5'){
      bytes.push(0x30);
      bytes.push(0x35);
    } else if (e == '6'){
      bytes.push(0x30);
      bytes.push(0x36);
    } else if (e == '7'){
      bytes.push(0x30);
      bytes.push(0x37);
    } else if (e == '8'){
      bytes.push(0x30);
      bytes.push(0x38);
    } else if (e == '9'){
      bytes.push(0x30);
      bytes.push(0x39);
    } else if (e == '十'){
      bytes.push(0x31);
      bytes.push(0x30);
    } else if (e == '百'){
      bytes.push(0x31);
      bytes.push(0x31);
    } else if (e == '千'){
      bytes.push(0x31);
      bytes.push(0x32);
    } else if (e == '万'){
      bytes.push(0x31);
      bytes.push(0x33);
    } else if (e == '点'){
      bytes.push(0x31);
      bytes.push(0x34);
    } else if (e == '元'){
      bytes.push(0x31);
      bytes.push(0x36);
    }
  }

  bytes.push(0x55);

  console.log("voice");
  console.log(bytes.length);
  console.log(bytes);
  return bytes;
}

function buf2string(buffer) {
  var arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
  return arr.map((char, i) => {
    return String.fromCharCode(char);
  }).join('');
}

function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}

function parseScore(score) {
  const levelInfo = [
    {min : 0, max : 10},
    {min : 11, max : 25},
    {min : 26, max : 45},
    {min : 46, max : 75},
    {min : 76, max : 100},
    {min : 101, max : 145},
    {min : 146, max : 190},
    {min : 191, max : 240},
    {min : 241, max : 290},
    {min : 291, max : 340},
    {min : 341, max : 420},
    {min : 421, max : 500},
    {min : 501, max : 580},
    {min : 581, max : 680},
    {min : 681, max : 780},
  ]

  var result = {
    level : 15,
    progress : 100
  }

  for (let i = 0; i < levelInfo.length; i++) {
    const element = levelInfo[i];
    if (score >= element.min && score <= element.max) {
      result.level = i + 1;
      result.progress = parseInt((score - element.min) * 100 / (element.max - element.min))
    }
  }

  return result
}

module.exports = {
  formatTime: formatTime,
  amountInWords : amountInWords,
  parsePriceBytes : parsePriceBytes,
  parseVoiceBytes : parseVoiceBytes,
  buf2string : buf2string,
  ab2hex : ab2hex,
  formatYYYYMMDD : formatYYYYMMDD,
  parseScore : parseScore
}
